#!/bin/sh /etc/rc.common

USE_PROCD=1
START=99

CONF="rustdesk-server"
WORKDIR="/etc/rustdesk-server"

start_service() {
	local auto_fw web_client
	local hbbr_enabled hbbr_port hbbs_enabled hbbs_port
	local hbbr_tcp_ports hbbs_tcp_ports hbbs_udp_ports

	config_load "$CONF"
	config_get_bool auto_fw "firewall" "auto_fw" "0"
	config_get_bool web_client "firewall" "web_client" "0"
	config_get_bool hbbr_enabled "hbbr" "enabled" "0"
	config_get hbbr_port "hbbr" "port" "21117"
	config_get_bool hbbs_enabled "hbbs" "enabled" "0"
	config_get hbbs_port "hbbs" "port" "21116"

	[ "$hbbr_enabled" -eq "1" ] || [ "$hbbs_enabled" -eq "1" ] || return 1

	if [ ! -d "$WORKDIR" ] && ! mkdir -p "$WORKDIR"; then
		logger -p daemon.error -t "rustdesk-server" "Failed to create working directory: $WORKDIR"
		return 1
	fi
	chown -R rustdesk-server:rustdesk-server "$WORKDIR"

	if [ "$hbbr_enabled" -eq "1" ]; then
		procd_open_instance "hbbr"
		procd_set_param command sh -c "cd $WORKDIR && /usr/bin/hbbr -p ${hbbr_port}"
		procd_set_param respawn
		procd_set_param env HOME="$WORKDIR"
		procd_set_param user rustdesk-server
		procd_set_param group rustdesk-server

		if [ "$auto_fw" -eq "1" ]; then
			if [ "$web_client" -eq "0" ]; then
				hbbr_tcp_ports="$hbbr_port"
			else
				hbbr_tcp_ports="$hbbr_port $(( hbbr_port + 2 ))"
			fi

			procd_open_data
			json_add_array firewall
				json_add_object ""
				json_add_string type rule
				json_add_string name "Allow-rustdesk-relay-tcp"
				json_add_string proto "tcp"
				json_add_string src "wan"
				json_add_string dest_port "$hbbr_tcp_ports"
				json_add_string target "ACCEPT"
				json_close_object
			json_close_array
			procd_close_data
		fi
		procd_close_instance
	fi

	if [ "$hbbs_enabled" -eq "1" ]; then
		procd_open_instance "hbbs"
		procd_set_param command sh -c "cd $WORKDIR && /usr/bin/hbbs -p ${hbbs_port}"
		procd_set_param respawn
		procd_set_param env HOME="$WORKDIR"
		procd_set_param user rustdesk-server
		procd_set_param group rustdesk-server

		if [ "$auto_fw" -eq "1" ]; then
			if [ "$web_client" -eq "0" ]; then
				hbbs_tcp_ports="$(( hbbs_port - 1 )) $hbbs_port"
			else
				hbbs_tcp_ports="$(( hbbs_port - 1 )) $hbbs_port $(( hbbs_port + 2 ))"
			fi
			hbbs_udp_ports="$hbbs_port"

			procd_open_data
			json_add_array firewall
				json_add_object ""
				json_add_string type rule
				json_add_string name "Allow-rustdesk-server-tcp"
				json_add_string proto "tcp"
				json_add_string src "wan"
				json_add_string dest_port "$hbbs_tcp_ports"
				json_add_string target "ACCEPT"
				json_close_object

				json_add_object ""
				json_add_string type rule
				json_add_string name "Allow-rustdesk-server-udp"
				json_add_string proto "udp"
				json_add_string src "wan"
				json_add_string dest_port "$hbbs_udp_ports"
				json_add_string target "ACCEPT"
				json_close_object
			json_close_array
			procd_close_data
		fi
		procd_close_instance
	fi
}

service_started() {
	procd_set_config_changed firewall
}

service_stopped() {
	procd_set_config_changed firewall
}

service_triggers() {
	procd_add_reload_trigger "$CONF"
}
